package com.zhihuishu.micro.course.service.impl;

import org.springframework.stereotype.Service;

import redis.clients.jedis.Jedis;

import com.zhihuishu.micro.course.dto.LessonVideoDto;
import com.zhihuishu.micro.course.service.LessonVideoTreenityRedisService;
import com.zhihuishu.toolkit.jedis.BeanWrapper;
import com.zhihuishu.toolkit.jedis.JedisHelper;
import com.zhihuishu.toolkit.jedis.JedisHelper.Handler;
import com.zhihuishu.toolkit.jedis.JedisHelper.ReturnHandler;

@Service("lessonVideoAllServiceRedis")
public class LessonVideoAllServiceRedisImpl extends BaseTreenityBatchServiceRedisImpl<LessonVideoDto> implements
        LessonVideoTreenityRedisService {
    @Override
    protected Class<LessonVideoDto> getDTOClass() {
        return LessonVideoDto.class;
    }

    @Override
    public String getObjectKey(Integer lessonId) {
        return JedisHelper.key("cc:lv", lessonId);
    }

    @Override
    public String getIdsKey(long courseId) {
        return JedisHelper.key("cc:course", courseId, "lvs");
    }

    @Override
    public void updateVideoIdRedis(final Integer videoId, final Integer lessonId, final Integer lessonVideoId) {

        

      
        jedisExecutor.execute(new Handler() {
            
            @Override
            public void handle(Jedis jedis) {
                String key = null;
                if (lessonVideoId != null) {
                    // 小节
                    key = "cc:video:" + videoId + ":lvs";
                }
                if (lessonId != null) {
                    // 节
                    key = "cc:video:" + videoId + ":lessons";
                }
                if (lessonVideoId != null) {
                    // 添加小节
                    jedis.sadd(key, lessonVideoId.toString());
                }
                if (lessonId != null) {
                    // 添加节
                    jedis.sadd(key, lessonId.toString());
                }
                
            }
        });

    }

    @Override
    public Long deleteVideoIdRedis(final Integer videoId,final Integer lessonId, final Integer lessonVideoId) {
     
        BeanWrapper<Long> executeAndReturn = jedisExecutor.executeAndReturn(new ReturnHandler<Long>() {

            @Override
            public Long handle(Jedis jedis) {
                Long ret = 0L;
                String lvsKey = null;
                String lessonKey = null;
                if (lessonVideoId != null) {
                    // 小节
                    lvsKey = "cc:video:" + videoId + ":lvs";
                }
                if (lessonId != null) {
                    // 节
                    lessonKey = "cc:video:" + videoId + ":lessons";
                }
                if (lvsKey != null) {
                    // 移除小节
                    ret=jedis.srem(lvsKey, lessonVideoId.toString());
                    return ret;
                }
                if (lessonKey != null) {
                    // 移除节
                    ret=jedis.srem(lessonKey, lessonId.toString());
                    return ret;
                }
                return null;
            }
            
        });
        return executeAndReturn.getValue();
    }

}
